﻿using RndMaterialDisposalService.Models.RequestModel;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace RndMaterialDisposalService.Models
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 
        /// </summary>
        public T? Data { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string? Message { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static ApiResponse<T> Fail(string errorMessage)
        {

            return new ApiResponse<T>() { Code = 500, Message = errorMessage };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResponse<T> Success(T data)
        {
            return new ApiResponse<T>() { Code = 200, Data = data };
        }



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string ToJsonString() {
            var data = JsonSerializer.Serialize(this, new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }) ;
            return data;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResponsePageList<T> : ApiResponse<T> {

        /// <summary>
        /// 
        /// </summary>
        public PageInfo? PageInfo { get; set; }

        /// <summary>
        /// 获取全量数据源分页数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static ApiResponse<List<T>> CreatePageList(List<T> list, int pageNum, int pageSize)
        {
            PageInfo pageInfo = new PageInfo();
            pageInfo.TotalPages = list.Count % pageSize == 0 ? list.Count / pageSize : list.Count / pageSize + 1;
            pageInfo.Total = list.Count;
            pageInfo.PageSize = pageSize;
            pageInfo.PageNum = pageNum;
            var result = list.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
            return new ApiResponsePageList<List<T>>() { Code = 200, Data = result, PageInfo = pageInfo };
        }
        /// <summary>
        /// 获取以分页数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static ApiResponse<List<T>> CreatePageListByResult(List<T> list, int pageNum, int pageSize,int totalCount)
        {
            PageInfo pageInfo = new PageInfo();
            pageInfo.TotalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            pageInfo.Total = totalCount;
            pageInfo.PageSize = pageSize;
            pageInfo.PageNum = pageNum;
            return new ApiResponsePageList<List<T>>() { Code = 200, Data = list, PageInfo = pageInfo };
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class PageInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public int PageNum { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int TotalPages { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Total { get; set; }
    }
}
